package com.dhecp.project.hr.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.common.utils.RandomValueUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.common.service.UpdateLogService;
import com.dhecp.project.common.service.impl.BaseModuleServiceImpl;
import com.dhecp.project.finance.mapper.PartnerBookedApplyMapper;
import com.dhecp.project.hr.domain.OperateCompanyRatio;
import com.dhecp.project.hr.domain.OperateCompanyRoute;
import com.dhecp.project.hr.mapper.OperateCompanyRatioMapper;
import com.dhecp.project.hr.mapper.OperateCompanyRouteMapper;
import com.dhecp.project.hr.service.OperateCompanyRouteService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * @program: kjecp
 * @description: 运营公司路径
 * @author: hlf
 * @create: 2022-10-20 14:15
 */
@Service
public class OperateCompanyRouteServiceImpl extends BaseModuleServiceImpl implements OperateCompanyRouteService {

    @Autowired
    private OperateCompanyRouteMapper businessMapper;
    @Autowired
    private OperateCompanyRatioMapper ratioMapper;
    @Autowired
    private PartnerBookedApplyMapper partnerBookedApplyMapper;//汇款数

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

    @Autowired
    private UpdateLogService updateLogService;

    /**
     * 查询
     *
     * @param params
     * @return List - OperateCompanyRoute
     * @throws Exception
     * @author DH-Xxj
     */
    @Override
    public List<OperateCompanyRoute> queryList(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");    //查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));    //常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));    //数据权限查询条件SQL语句

//        String otherWhereStr = "";
//        if ((params.get("otherWhere") == null || params.get("otherWhere") == "") && ShiroUtils.getMandatorLevel() < 6){
//            otherWhereStr = "o1.CUser = '" + ShiroUtils.getMandatorId() + "'";
//        }
//
//        if (!"".equals(otherWhereStr)){
//            params.put("otherWhereStr", otherWhereStr);
//        }

        PageUtils.startPage();
        return businessMapper.select(params);
    }

    /**
     * @Description: 查询指定单nid的首记录
     * @Param: [params]
     * @return: com.dhecp.project.storage.domain.MaterialMaintenance
     * @Author: hlf
     * @date: 2022-05-09 10:10
     */
    @Override
    public OperateCompanyRoute findByGuid(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");    //查询表别名
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));    //数据权限查询条件SQL语句
        return businessMapper.findById(params);
    }

    /**
     * @Description: 查询单条记录（通过公司简称）
     * @Author: XDH
     */
    @Override
    public OperateCompanyRoute findByShortName(String shortName){
        return businessMapper.findByShortName(shortName);
    }

    /**
     * @Description: 查询指定单nid的首记录
     * @Param: [params]
     * @return: com.dhecp.project.storage.domain.MaterialMaintenance
     * @Author: hlf
     * @date: 2022-05-09 10:10
     */
    @Override
    public OperateCompanyRatio findByRatioGuid(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");    //查询表别名
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));    //数据权限查询条件SQL语句
        return ratioMapper.findById(params);
    }
//
//    /**
//     * @Description: 查询指定单nid的首记录
//     * @Param: [params]
//     * @return: com.dhecp.project.storage.domain.MaterialMaintenance
//     * @Author: hlf
//     * @date: 2022-05-09 10:10
//     */
//    @Override
//    public OperateCompanyRatio ratioTop1(Map<String, Object> params) {
//        return ratioMapper.ratioTop1(params);
//    }

    /**
     * @Description: 运营比例修改
     * @Param: [params] moduleTable 必要参数：功能唯一标识
     * @Param: [params] infoMap 必要参数：非网格字段（包括头部、底部所有显示和隐藏的字段）
     * @Param: [params] infoItems 必要参数：对应实体类的json格式字符串
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: hlf
     * @date: 2022-05-09 10:10
     */
    @SuppressWarnings("unchecked")
    @Override
    public AjaxResult ratioSave(Map<String, Object> params) {
        OperateCompanyRatio oldData = new OperateCompanyRatio();
        OperateCompanyRatio infoItems = JSON.parseObject(params.get("infoItems").toString(), OperateCompanyRatio.class);

        Map<String, Object> params2 = new HashMap<String, Object>();
        params2.put("guid", infoItems.getGuid());
        oldData = ratioMapper.findById(params2);
        updateLogService.addUpdateLog(params.get("moduleTable").toString(), infoItems.getGuid(), oldData, infoItems, "修改");

        if (infoItems.getGuid() != null && !infoItems.getGuid().equals("")) {
            infoItems.setUpdateTime(new Date());
            infoItems.setUpdateUser(ShiroUtils.getUserId());
            ratioMapper.update(infoItems);
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"), infoItems.getGuid());
    }

    /**
     * @Description: 保存
     * @Param: [params] moduleTable 必要参数：功能唯一标识
     * @Param: [params] infoMap 必要参数：非网格字段（包括头部、底部所有显示和隐藏的字段）
     * @Param: [params] infoItems 必要参数：对应实体类的json格式字符串
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: hlf
     * @date: 2022-05-09 10:10
     */
    @SuppressWarnings("unchecked")
    @Override
    public AjaxResult saveData(Map<String, Object> params) {
        OperateCompanyRoute oldData = new OperateCompanyRoute();
        List<OperateCompanyRoute> infoItems = JSON.parseArray(params.get("infoItems").toString(), OperateCompanyRoute.class);

        for (OperateCompanyRoute item : infoItems
        ) {
            Map<String, Object> params2 = new HashMap<String, Object>();
            params2.put("guid", item.getGuid());
            oldData = businessMapper.findById(params2);

            item.setvYyBl(getRatio(item));
            item.setvJcBl(item.getvYyBl() > item.getvTsBl() ? item.getvYyBl() : item.getvTsBl());

            if (item.getGuid() != null && !item.getGuid().equals("")) {

                businessMapper.update(item);
                updateLogService.addUpdateLog(params.get("moduleTable").toString(), item.getGuid(), oldData, item, "修改");
            } else {
                item.setGuid(RandomValueUtils.getGUID());
                businessMapper.deleteByGsbm(item.getvGsBm());
                businessMapper.insert(item);
                updateLogService.addInsertLog(params.get("moduleTable").toString(), item.getGuid(), "新增");
            }
        }

        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }

    private Double getRatio(OperateCompanyRoute item) {
        Map<String, Object> params2 = new HashMap<String, Object>();
        double jcBl;//基础比例
        double hkBl;//回款比例
        double dpBl;//店铺比例

//        params2.put("vGsBm", item.getvGsBm());
        OperateCompanyRatio operateCompanyRatio = ratioMapper.ratioTop1();
        jcBl = operateCompanyRatio!=null && operateCompanyRatio.getvJcBl2()>0?operateCompanyRatio.getvJcBl2():0;

//        List<OperateCompanyRatio> operateCompanyRatios = ratioMapper.select(params2);//获取比例设置
//        //获取店铺数
//        //获取回款数
//        for (OperateCompanyRatio ratio : operateCompanyRatios
//        ) {
//
//
//        }


//        jcBl = operateCompanyRatio.getvJcBl();
//        double x = partnerBookedApplyMapper.getAmountCashed(params2);
//        x = Math.floor(x / operateCompanyRatio.getvHks())*0.5;
//        hkBl = x > operateCompanyRatio.getvHkBl() ? operateCompanyRatio.getvHkBl() : x;
//        dpBl = 0.00;
//        return jcBl + hkBl + dpBl;
        return jcBl;
    }

}